home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / doc / example.man < prev    next >
Text File  |  1992-08-03  |  2KB  |  73 lines

  1. .SH EXAMPLE
  2. .PP
  3. Using the \*(NM library is relatively easy \- you need to construct your
  4. arguments, your command-line, and your argument iterator.  Then all that is
  5. left to do is call the \f4parse\fP member function of your \*(NM object.
  6. The following is a simple example:
  7.  
  8. .XS
  9. #include <stdlib.h>
  10. #include <iostream.h>
  11. #include <cmdargs.h>
  12.  
  13. int  main(int argc, char * argv[])
  14. {
  15.       // Declare arguments
  16.    CmdArgInt  count('c', "count", "number",
  17.                        "number of copies to print.");
  18. .sp 4p
  19.    CmdArgBool xflag('x', "xmode",
  20.                        "turn on 'x'-mode.");
  21. .sp 4p
  22.    CmdArgChar fdsep('s', "separator", "char",
  23.                        "field-separator to use.");
  24. .sp 4p
  25.    CmdArgStr  input("input-file",
  26.                        "input file to read.");
  27. .sp 4p
  28.    CmdArgStrList  output("[output-file \*(..]",
  29.                             "where to print output.");
  30.  
  31.       // Declare the command and the argument-iterator
  32.    CmdLine  cmd(*argv,
  33.                    &count, &xflag, &fdsep,
  34.                    &input, &output, NULL);
  35. .sp 4p
  36.    CmdArgvIter  arg_iter(--argc, ++argv);
  37.  
  38.       // Initialize arguments to appropriate default values.
  39.    count = 1;
  40.    xflag = 0;
  41.    fdsep = ',';
  42.  
  43.       // Parse arguments
  44.    cmd.parse(arg_iter);
  45.  
  46.       // Print arguments
  47.    cout << "count=" << count << endl ;
  48.    cout << "xflag=" << (xflag ? "ON" : "OFF") << endl ;
  49.    cout << "fdsep='" << (char) fdsep << "'" << endl ;
  50.    cout << "input=\\"" << input << "\\"" << endl ;
  51.    
  52.    for (int i = 0 ; i < output.count() ; i++) {
  53.       cout << "output[" << i << "]=" << output[i] << endl ;
  54.    }
  55.  
  56.    return  0;
  57. }
  58. .XE
  59.  
  60. The Unix command-line syntax for the above program would be as follows:
  61.  
  62. .XS
  63. progname [\-c number] [\-x] [\-s char] input-file [output-file \*(..]
  64. .XE
  65.  
  66. The Unix command-line syntax using long-options (keywords) for the above
  67. program would be as follows:
  68.  
  69. .XS
  70. progname [\*(--count number] [\*(--xmode] [\*(--separator char]
  71.          input-file [output-file \*(..]
  72. .XE
  73.